<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@page import="jdbc.JdbcDao"%>
<%@page import = "email.*" %>
<%@page import = "java.text.SimpleDateFormat" %>
<%@ page import = "java.sql.Statement" %>

<%@ include file = "login.jsp" %>


<%

//不要加文件头, ajax 需要纯数据

String email = request.getParameter("email");
String password = request.getParameter("password");
String admin = request.getParameter("admin");
String realname = request.getParameter("realname");
String birthdate = request.getParameter("birthdate");
String nickname = request.getParameter("nickname");
String gender = request.getParameter("gender");
String phonenumber = request.getParameter("phonenumber");
String emercont = request.getParameter("emercont");
String emercontnum = request.getParameter("emercontnum");
String address = request.getParameter("address");
String qq = request.getParameter("qq");

// 如果已经登陆了, 把注册请求当做修改个人信息请求
if (email_status != null) {
	if (admin == null)
		admin = user_info.get("admin");
	System.out.println(email + ", " + email_status);
	if (!("admin".equals(user_info.get("admin")) && email != null && !("".equals(email))))
		email = email_status;
	System.out.println(email + ", " + email_status);
	
	JdbcDao mysql = new JdbcDao();
	//mysql.clearConnection();
	mysql.excuteCommand("use info;");
	Statement s = mysql.excuteCommand("UPDATE users set " +
		("??????".equals(password) ? "" : "password='" + String.valueOf(password.hashCode()) + "',") +
		"admin='" + admin + "'," +
		"nickname='" + nickname + "'," +
		"realname='" + realname + "'," +
		"gender='" + gender + "'," +
		"phonenumber='" + phonenumber + "'," +
		"emercont='" + emercont + "'," +
		"emercontnum='" + emercontnum + "'," +
		"address='" + address + "'," +
		"qq='" + qq + "'," +
		"birthdate='" + birthdate + "'" +
		"where email='" + email + "';"
	);
	if (s != null)
		out.println("success,修改个人信息成功.");
	else
		out.println("error,修改个人信息失败.");
} else {


	if (CheckEmail.checkEmail(email) == false) {
		out.println("您输入的邮箱格式不正确，或已经被注册.");
	}
	else {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		java.util.Date dDate = sdf.parse(birthdate.trim());
		//java.util.Date dDate = sdf.parse("1993-06-10");
		java.sql.Date sDate = new java.sql.Date(dDate.getTime());

		String validcode = GetValidCode.getValidCode(); 

		JdbcDao mysql = new JdbcDao();
		//mysql.clearConnection();
		mysql.excuteCommand("use info;");
		mysql.excuteCommand("INSERT INTO users (" +
			"email," +
			"admin," +
			"password," +
			"nickname," +
			"realname," +
			"gender," +
			"phonenumber," +
			"emercont," +
			"emercontnum," +
			"address," +
			"qq," +
			"validcode," + 
			"birthdate" +
			")" +
			" VALUES(" +
			"'" + email + "'," +
			("admin".equals(realname)? "'admin'," : "'invalid',") +
			"'" + String.valueOf(password.hashCode()) + "'," +
			"'" + nickname + "'," +
			"'" + realname + "'," +
			"'" + ("male".equals(gender) ? "male" : "female") + "'," +
			"'" + phonenumber + "'," +
			"'" + emercont + "'," +
			"'" + emercontnum + "'," +
			"'" + address + "'," +
			"'" + qq + "'," +
			"'" + validcode + "'," +
			"'" + sDate + "'" +//don't know if it's right.
			");"
		);
		
		if (SendCheckEmail.sendCheckEmail(email, validcode))
			out.println("success,已发送验证邮件到您的邮箱!");
		else
			out.println("error,邮箱格式不正确");

	}
}
//}

//TODO: no hard code here
//response.sendRedirect("/Info/index.jsp");

%>